<?php
/**
 * Created by PhpStorm.
 * User: sleep
 * Date: 2018/3/31
 * Time: 下午4:34
 */

namespace app\common\exception;

use think\exception\Handle;
use think\exception\HttpResponseException;
use think\Response;
use Throwable;

class ApiHandle extends Handle
{
    public function render($request, Throwable $e): Response
    {
        if($e instanceof HttpResponseException) {
            return parent::render($request, $e);
        }else {
            if($request->isGet()) {
                pp($e->getMessage());
                pp([
                    'file' => $e->getFile(),
                    'line' => $e->getLine()
                ]);
            }else {
                echo json_encode([
                    'message' => $e->getMessage(),
                    'code' => 1000,
                    'data' => [
                        'file'=>$e->getFile(),
                        'line'=>$e->getLine(),
                    ],
                    'cmd' => $request->controller() . '/' . $request->action()
                ]);
            }
            exit();
        }
    }

}